package com.countryside.controller;

import com.countryside.pojo.*;
import com.countryside.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    EmployeeWorkService employeeWorkService;
    @Autowired
    EmployeeProjectService employeeProjectService;
    @Autowired
    CompanyWorkService companyWorkService;
    @Autowired
    CompanyService companyService;

    /**
     * 进入登录页面
     * @return
     */
    @RequestMapping(value = {"/","/login"} , method = RequestMethod.GET)
    public String toLogin(){
        return "login";
    }

    /**
     * 进入个人主页
     * @return
     */
    @GetMapping("/resume")
    public String resume() {
        return "resume";
    }


    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @param model
     * @return
     */
    @PostMapping(value = {"/login"})
    public String login(@RequestParam("userName") String username,
                        @RequestParam("password") String password,
                        HttpSession session,
                        Model model) {
        //
        User exist = userService.queryUserByUsername(username);
        User user = userService.queryUserByUsernameAndPassword(username, password);
        if (user!=null){
            //登录成功，将user保存到session域
            session.setAttribute("user", user);
            return "redirect:/index";
        } else if(exist==null){
            model.addAttribute("username",username);
            model.addAttribute("msg","用户名不存在");
            return "login";
        } else{
            model.addAttribute("username",username);
            model.addAttribute("msg","账号密码不正确");
            return "login";
        }
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @GetMapping("/exitUser")
    public String exitUser(HttpSession session) {
        session.removeAttribute("user");
        return "login";
    }

    /**
     * 进入注册页面
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String toRegister(){
        return "register";
    }

    /**
     * 用户注册
     * @param user
     * @param model
     * @return
     */
    @PostMapping(value = "/register")
    public String register(User user, Model model){
        User exist = userService.queryUserByUsername(user.getUserName());
        if (exist!=null){
            model.addAttribute("msg","用户名已存在");
            model.addAttribute("username",user.getUserName());
            return "register";
        }else {
            userService.addUser(user);
            return "redirect:login";
        }
    }

    /**
     * 修改用户工人信息
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/updateUser")
    public String updateUser(User user, HttpSession session) {
        User thisUser = (User) session.getAttribute("user");
        user.setId(thisUser.getId());
        user.setUserName(thisUser.getUserName());
        user.setPassword(thisUser.getPassword());
        user.setUserImg(thisUser.getUserImg());
        userService.updateUser(user);
        session.setAttribute("user", user);
        return "redirect:getEmployeeWork";
    }

    /**
     * 获取用户项目经验
     * @param session
     * @param model
     * @return
     */
    @GetMapping("/getEmployeeWork")
    public String getEmployeeWork(HttpSession session,
                                  Model model) {
        //从session域取到用户信息
        User user = (User) session.getAttribute("user");
        EmployeeWork employeeWork = employeeWorkService.getEmployeeWork(user.getId());
        List<EmployeeProject> employeeProjects = employeeProjectService.getEmployeeProjectByUserId(user.getId());
        Company company = companyService.getCompany(user.getId());
        if (company != null) {
            List<CompanyWork> companyWorks = companyWorkService.queryWorkByCompanyId(company.getId());
            System.out.println(companyWorks);
            model.addAttribute("companyWorks", companyWorks);

        }
        model.addAttribute("employeeProjects", employeeProjects);
        model.addAttribute("employeeWork", employeeWork);
        model.addAttribute("company", company);
        return "resume";
    }

    /**
     * 修改用户头像
     * @param headerImg
     * @param session
     * @param model
     * @return
     * @throws IOException
     */
    @PostMapping("/updateUserImg")
    public String updateUserImg(@RequestPart("headerImg") MultipartFile headerImg,
                                HttpSession session,
                                Model model) throws IOException {
        //从session域取到用户信息
        User user = (User) session.getAttribute("user");
        //获取上传文件名
        String originalFilename = headerImg.getOriginalFilename();
        //获取当前项目的绝对路径
        String thisPath = System.getProperty("user.dir");
        //获取上传文件的后缀
        String imgSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //指定只能上传格式为jpg、png或bmp的图片
        if (imgSuffix.equals(".jpg") || imgSuffix.equals(".png") || imgSuffix.equals(".bmp")) {
            //生成一个新的唯一文件名
            String newFilename = UUID.randomUUID().toString() + imgSuffix;
            //上传文件到指定目录
            headerImg.transferTo(new File(thisPath + "\\src\\main\\resources\\static\\uploadImg\\" + newFilename));
            //从数据库中更新用户头像
            userService.updateImgByUserId(user.getId(), newFilename);
            //再把更新后的user重新放到session域中
            user.setUserImg(newFilename);
            session.setAttribute("user", user);
            return "redirect:getEmployeeWork";
        } else {
            return "redirect:getEmployeeWork";
        }
    }


}

